package nine.test_9_7;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            long n = scanner.nextLong();
            long m = scanner.nextLong();
            boolean b = false;
            if(n == 1) {
                System.out.print(0 + " ");
                continue;
            }
            if(n % 2 != 0) {
                n = n / 2 + 1;
            } else {
                n = n / 2;
            }
            long max = 0L;
            if(n == 1) {
                System.out.println(m + " ");
                continue;
            }
            for (long j = n; j > 0; j--) {
                long tmp = m % j;
                long count = m / j;
                if (j == n){
                    max = Math.max(max,count * (2 * j - 2) + 2 * tmp);
                } else {
                    max = Math.max(max,count * (2 * j) + 2 * tmp);
                }
            }
            System.out.print(max + " ");
        }
    }
}
